package J5922;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 周赛：
 * 统计出现过一次的公共字符串
 */
public class Test {
    public static void main(String[] args) {
        System.out.println(new Solution().countWords(new String[]{"leetcode","is","amazing","as","is"},
                new String[]{"amazing","leetcode","is"}));
    }
}

class Solution {
    public int countWords(String[] words1, String[] words2) {
        int count = 0;
        Map<String,Integer> res = new HashMap<>();
        for (int i = 0; i < words1.length; i++) {
            if(!res.containsKey(words1[i])){
                res.put(words1[i],1);
            }else {
                int num = res.get(words1[i]);
                res.put(words1[i],++num);
            }
        }

        Map<String,Integer> res1 = new HashMap<>();
        for (int i = 0; i < words2.length; i++) {
            if(!res1.containsKey(words2[i])){
                res1.put(words2[i],1);
            }else {
                int num = res1.get(words2[i]);
                res1.put(words2[i],++num);
            }
        }

        for (String s : res.keySet()) {
            if(res.get(s) == 1){
                if(res1.containsKey(s)){
                    if(res1.get(s) == 1){
                        count++;
                    }
                }else {
                    continue;
                }
            }
        }

        return count;
    }
}
